package com.travelmood.controller;

import com.travelmood.pojo.Image;
import com.travelmood.pojo.Result;
import com.travelmood.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/images")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @PostMapping("/add")
    public Result addImage(@RequestBody Image image, @RequestParam Integer placeId) {
        try {
            imageService.addImage(image);
            return Result.success();
        } catch (Exception e) {
            return Result.error("发生错误，无法添加图片");
        }
    }

    @PutMapping("/update")
    public Result updateImage(@RequestBody Image image) {
        try {
            imageService.updateImage(image);
            return Result.success();
        } catch (Exception e) {
            return Result.error("发生错误，无法更新图片");
        }
    }

    @DeleteMapping("/delete")
    public Result deleteImage(@RequestParam Integer imageId) {
        try {
            imageService.deleteImageById(imageId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("发生错误，无法删除图片");
        }
    }

    @GetMapping("/get")
    public Result getImage(@RequestParam Integer imageId) {
        try {
            Image image = imageService.findImageById(imageId);
            return Result.success(image);
        } catch (Exception e) {
            return Result.error("发生错误，无法查询图片");
        }
    }

    @GetMapping("/all")
    public Result getAllImages() {
        try {
            List<Image> images = imageService.getAllImages();
            return Result.success(images);
        } catch (Exception e) {
            return Result.error("发生错误，无法获取图片列表");
        }
    }
    @GetMapping("/byPlaceId")
    public Result getImagesByPlaceId(@RequestParam Integer placeId) {
        try {
            List<Image> images = imageService.findImagesByPlaceId(placeId);
            return Result.success(images);
        } catch (Exception e) {
            return Result.error("发生错误，无法获取地点的图片列表");
        }
    }
}